package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpSoDao;
import com.cowain.monitor.dao.PmsCheckDemandDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
//import com.cowain.monitor.datasources.annotation.OperLog;
import com.cowain.monitor.entity.ErpCompleteEntity;
import com.cowain.monitor.entity.PmsCheckDemandEntity;
import com.cowain.monitor.service.PmsCheckDemandService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import com.cowain.monitor.utils.R;
import com.cowain.monitor.utils.SubStringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * PMS ehr-backend
 *
 * @Classname PmsCheckDemandServiceImpl
 * @Description TODO
 * @Date 2020-07-28 13:43
 * @Created 沈科举
 */
@Service
public class PmsCheckDemandServiceImpl extends ServiceImpl<PmsCheckDemandDao, ErpCompleteEntity> implements PmsCheckDemandService {
    @Autowired
    private ErpSoDao erpSoDao;

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpCompleteEntity> findErpCmpleteEntityByIsdel() {
        int totalCount = baseMapper.totalCount();
        int totalPage=(totalCount/3000)+1;
        List<ErpCompleteEntity> list=new ArrayList<>();
        for (int i = 1; i <=totalPage ; i++) {
            list.addAll(baseMapper.findErpCmpleteEntityByIsdel(i));
        }

        return list;
    }


    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public R addPmsCheckDemand(List<ErpCompleteEntity> erpCompleteEntities) {
        int result = 0;
        List<PmsCheckDemandEntity> list=new ArrayList<>();
        List<ErpCompleteEntity> updataErpCompleteList=new ArrayList<>();
        if (null != erpCompleteEntities && 0 != erpCompleteEntities.size()) {
            erpCompleteEntities.forEach(e -> {
                PmsCheckDemandEntity p = new PmsCheckDemandEntity();
                int length = e.getProjectno().length();
                if (8 == length) {
                String projectNo = SubStringUtils.jointProjectNo(e.getProjectno());
                Long projectId = erpSoDao.findProjectNo(projectNo);
                if (null != projectId) {
                    p.setProjectId(projectId);
                    p.setDemandTime(e.getIndate());
                    p.setBatch(e.getModoc());
                    list.add(p);
                    updataErpCompleteList.add(e);
                }
            }
            });
            if(null!=list&& 0!=list.size()){
                result = baseMapper.addPmsCheckDemand(list);
            }

        }
        return R.ok().put("result",result).put("updataErpCompleteList",updataErpCompleteList);
    }


    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public int updateErpCompleteEntityIsdel(List<ErpCompleteEntity> erpCompleteEntity) {
        int result = 0;
        if (null != erpCompleteEntity && 0 != erpCompleteEntity.size()) {
            for (ErpCompleteEntity e : erpCompleteEntity) {
                e.setIsdel(PmsConstantUtils.IS_DEL_1.intValue());
                result = baseMapper.updateErpCompleteEntityIsdel(e);
            }
        }
        return result;
    }
}
